using System;
using System.Windows.Forms;

namespace Mbs.Windows.Elements
{
	
	public class ToolStripItemElement : ToolStripItemCollectionElement
	{
		ToolStripItem item;

		
		public ToolStripItemElement(ToolStripItem item)
			: base(item.Owner.Items)
		{
			this.item = item;
			item.OwnerChanged += new EventHandler(item_OwnerChanged);
		}

        //public override void RefreshResource()
        //{
        //    base.RefreshResource();
        //    var g = item as Mbs.Globalization.ISupportGlobalization;
        //    if (g != null && g.IsSupportGlobalization)
        //        g.RefreshResource();
        //}

		protected override int GetInsertingIndex(object uiElement)
		{
			int index = InternalCollection.IndexOf(item);
			if (index < 0)
				throw new InvalidOperationException();

			return  index + 1;
		}

        public override string Name
        {
            get
            {
                return item.Name;
            }
            set
            {
                item.Name = value;
            }
        }

        public override string Text
        {
            get
            {
                return item.Text;
            }
            set
            {
                item.Text = value;
            }
        }

        public override object Tag
        {
            get
            {
                return item.Tag;
            }
            set
            {
                item.Tag = value;
            }
        }

        public override bool? Enabled
        {
            get
            {
                return item.Enabled;
            }
            set
            {
                item.Enabled = value.HasValue ? value.Value : false;
            }
        }

        public override bool? Visible
        {
            get
            {
                return item.Visible; 
            }
            set
            {
                item.Visible = value.HasValue ? value.Value : false;
            }
        }

		private void item_OwnerChanged(object sender, EventArgs e)
		{
			if (item.Owner == null)
				InternalCollection = null;
			else
				InternalCollection = item.Owner.Items;
		}

        
	}

    public class ToolStripSeparatorElement : ToolStripItemElement
    {
        ToolStripSeparator separator;

        public ToolStripSeparatorElement(ToolStripSeparator separator)
            : base(separator)
        {
            this.separator = separator;
            UpdateUI();
        }

        protected override ToolStripItem DoAdd(ToolStripItem uiElement)
        {
            ToolStripItem item = base.DoAdd(uiElement);
            UpdateUI();
            return item;
        }

        protected override void DoRemove(ToolStripItem uiElement)
        {
            base.DoRemove(uiElement);
            UpdateUI();
        }

       
        private void UpdateUI()
        {
            if (InternalCollection != null)
            {
                separator.Visible = (InternalCollection.IndexOf(separator) != InternalCollection.Count - 1);
            }
            else
            {
                separator.Visible = false;
            }
        }
    }

    public class ToolStripDropDownItemElement : ToolStripItemCollectionElement
    {
        private ToolStripDropDownItem dropDownItem;

        
        public ToolStripDropDownItemElement(ToolStripDropDownItem dropDownItem)
            : base(dropDownItem.DropDown.Items)
        {
            this.dropDownItem = dropDownItem;
        }

        //public override void RefreshResource()
        //{
        //    base.RefreshResource();
        //    var g = dropDownItem as Mbs.Globalization.ISupportGlobalization;
        //    if (g.IsSupportGlobalization)
        //        g.RefreshResource();
        //}

        

        public override bool? Enabled
        {
            get
            {
                return dropDownItem.Enabled;
            }
            set
            {
                dropDownItem.Enabled = value.HasValue ? value.Value : false;
            }
        }

        public override bool? Visible
        {
            get
            {
                return dropDownItem.Visible;
            }
            set
            {
                dropDownItem.Visible = value.HasValue ? value.Value : false;
            }
        }

    }
}
